package com.aitaoxue.palx.fragment;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.adapter.SearchAdapter;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.ListBaseAdapter;
import com.aitaoxue.palx.base.NoCacheBaseListFragment;
import com.aitaoxue.palx.bean.ListEntity;
import com.aitaoxue.palx.bean.Teacher;
import com.aitaoxue.palx.bean.TeacherList;
import com.aitaoxue.palx.ui.PersonCenterActivity;
import com.aitaoxue.palx.ui.empty.EmptyLayout;
import com.aitaoxue.palx.util.GsonUtil;

public class SearchResultFragment extends NoCacheBaseListFragment {

	// 筛选的条件
	private static final int FILTER_BY_PRICE = 1;
	private static final int FILTER_BY_DISTANCE = 2;
	private static final int FILTER_BY_SEX = 3;

	private OnSearchSuccessListener listener;

	// @InjectView(R.id.rl_default)
	protected RelativeLayout rl_default;
	// @InjectView(R.id.rl_price)
	protected RelativeLayout rl_price;
	// @InjectView(R.id.rl_distance)
	protected RelativeLayout rl_distance;
	// @InjectView(R.id.rl_sex)
	protected RelativeLayout rl_sex;
	// @InjectView(R.id.filter_default)
	protected TextView filter_default;
	// @InjectView(R.id.filter_price)
	protected TextView filter_price;
	// @InjectView(R.id.filter_distance)
	protected TextView filter_distance;
	// @InjectView(R.id.filter_sex)
	protected TextView filter_sex;

	public interface OnSearchSuccessListener {
		void searchSuccess();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		listener = (OnSearchSuccessListener) activity;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_pull_refresh_listview_with_filter;
	}

	@Override
	public void initView(View view) {
		rl_default = (RelativeLayout) view.findViewById(R.id.rl_default);
		rl_price = (RelativeLayout) view.findViewById(R.id.rl_price);
		rl_distance = (RelativeLayout) view.findViewById(R.id.rl_distance);
		rl_sex = (RelativeLayout) view.findViewById(R.id.rl_sex);
		filter_default = (TextView) view.findViewById(R.id.filter_default);
		filter_price = (TextView) view.findViewById(R.id.filter_price);
		filter_distance = (TextView) view.findViewById(R.id.filter_distance);
		filter_sex = (TextView) view.findViewById(R.id.filter_sex);

		super.initView(view);

		rl_default.setOnClickListener(this);
		rl_price.setOnClickListener(this);
		rl_distance.setOnClickListener(this);
		rl_sex.setOnClickListener(this);

	}

	protected static final String TAG = SearchResultFragment.class
			.getSimpleName();

	@Override
	protected ListBaseAdapter getListAdapter() {
		return new SearchAdapter();
	}

	@Override
	protected ListEntity parseList(byte[] response) throws Exception {
		String res = new String(response);

		TeacherList teacherList = GsonUtil.GsonToBean(res, TeacherList.class);

		if (teacherList != null) {
			listener.searchSuccess();
		}

		return teacherList;
	}

	@Override
	protected void sendRequestData(Map<String, String> map) {
		// 加上搜索词
		EditText search = (EditText) getActivity().findViewById(
				R.id.search_content);
		String keyWord = search.getText().toString();

		if (map == null) {
			map = new HashMap<String, String>();
		}
		map.put("generalsearch", keyWord);

		AitaoXueApi.search(String.valueOf(mCurrentPage), map, mCacheHandler);
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);

		switch (view.getId()) {
		case R.id.rl_default:// 默认
			filter_default.setTextColor(getResources().getColor(
					R.color.header_bg));
			filter_distance.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_price.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_sex.setTextColor(getResources().getColor(
					R.color.common_text_color));

			refreshData(null);
			break;
		case R.id.rl_price:// 价格
			filter_default.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_distance.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_price.setTextColor(getResources()
					.getColor(R.color.header_bg));
			filter_sex.setTextColor(getResources().getColor(
					R.color.common_text_color));

			showPopupWindow(view, FILTER_BY_PRICE);

			break;
		case R.id.rl_distance:
			filter_default.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_distance.setTextColor(getResources().getColor(
					R.color.header_bg));
			filter_price.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_sex.setTextColor(getResources().getColor(
					R.color.common_text_color));

			showPopupWindow(view, FILTER_BY_DISTANCE);
			break;
		case R.id.rl_sex:
			filter_default.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_distance.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_price.setTextColor(getResources().getColor(
					R.color.common_text_color));
			filter_sex.setTextColor(getResources().getColor(R.color.header_bg));

			showPopupWindow(view, FILTER_BY_SEX);
			break;

		default:
			break;
		}
	}

	private void showPopupWindow(View view, final int filter) {
		View filterWindow = LayoutInflater.from(getActivity()).inflate(
				R.layout.popupwindow_search, null);

		TextView option1 = (TextView) filterWindow
				.findViewById(R.id.option_first);
		TextView option2 = (TextView) filterWindow
				.findViewById(R.id.option_second);

		switch (filter) {
		case FILTER_BY_PRICE:
			option1.setText("由低到高");
			option2.setText("由高到低");
			break;
		case FILTER_BY_DISTANCE:
			option1.setText("由近及远");
			option2.setText("由远及近");
			break;
		case FILTER_BY_SEX:
			option1.setText("  男    ");
			option2.setText("  女    ");
			break;
		default:
			break;
		}

		final PopupWindow popupWindow = new PopupWindow(filterWindow,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

		// final PopupWindow popupWindow=new PopupWindow(filterWindow);
		popupWindow.setTouchable(true);

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});

		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.popup_search_background));

		popupWindow.showAsDropDown(view);

		option1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Map<String, String> map = new HashMap<String, String>();

				switch (filter) {
				case FILTER_BY_PRICE: // 由低到高
					map.put("bool", "1");
					break;
				case FILTER_BY_DISTANCE: // 由近及远
					map.put("bool", "2");
					break;
				case FILTER_BY_SEX: // 男
					map.put("sex", "1");
					break;
				}

				refreshData(map);
				popupWindow.dismiss();
			}
		});

		option2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Map<String, String> map = new HashMap<String, String>();

				switch (filter) {
				case FILTER_BY_PRICE: // 由高到低
					map.put("bool", "-1");
					break;
				case FILTER_BY_DISTANCE: // 由远及近
					map.put("bool", "-2");
					break;
				case FILTER_BY_SEX: // 女
					map.put("sex", "-1");
					break;
				}

				refreshData(map);
				popupWindow.dismiss();
			}
		});
	}

	private void refreshData(Map<String, String> map) {
		mListView.setSelection(0);
		mCurrentPage = 0;
		mState = STATE_REFRESH;
		mErrorLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
		requestData(true, map);
	}

	public void onSearchChanged() {
		refreshData(null);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(getActivity(), PersonCenterActivity.class);
		Teacher teacher = (Teacher) mAdapter.getItem(position);
		intent.putExtra("prod_id", teacher.getProd_id());
		intent.putExtra("isMine", false);
		startActivity(intent);
	}

}
